// 组件接口
class TextMessage{
  constructor(message) { 
    this.message = message;
  }

  getText() { 
    return this.message;
  }
}

// 装饰器的基类
class MessageDecorator{
  constructor(textMessage) { 
    this.textMessage = textMessage;
  }

  getText() { 
    return this.textMessage.getText();
  }
}

// 具体装饰器子类 HTML装饰器
class HTMLDecorator extends MessageDecorator{
  getText() { 
    const msg = super.getText();
    return `<p>${msg}</p>`
  }
}

// 具体装饰器子类 加密装饰器
class EncryptDecorator extends MessageDecorator{
  getText() { 
    const msg = super.getText();
    // 倒序加密
    return this.encrypt(msg);
  }
  encrypt(msg) { 
    return msg.split("").reverse().join("");
  }
}

// 使用
let message = new TextMessage("Hello World");
message = new HTMLDecorator(message);
message = new EncryptDecorator(message);

console.log(message.getText());